package tcln.gxtx.client.widget.grid.renderer.html;

import tcln.gxtx.client.utils.lang.GwtStringUtils;
import tcln.gxtx.client.widget.treegrid.model.AbstractTreeModel;

import com.extjs.gxt.ui.client.core.XDOM;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;

/**
 * A faster version of {@link TwoComponentHtmlRenderer} which reduces the rendered data if there is no renderer
 * registered. For example, if there is no left renderer, no DIV is created for this block. The same is applied to right
 * renderer.
 * 
 * @param <M>
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class FastTwoComponentHtmlRenderer<M extends AbstractTreeModel> extends TwoComponentHtmlRenderer<M> {

    /**
     * @param ownerColumn
     */
    public FastTwoComponentHtmlRenderer(ColumnConfig ownerColumn) {
        super(ownerColumn);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String render(M model, String property, ColumnData config, int rowIndex, int colIndex, ListStore<M> store,
            Grid<M> grid) {
        height = getHeight();
        width = getWidth();
        leftRegionSize = getLeftRegionSize();

        String wholeRenderedText = "";
        String uniqueId = XDOM.getUniqueId();
        HtmlGridCellRenderer<M> renderer;

        // render border-layout container
        wholeRenderedText += "<div id=\"" + uniqueId + "\" ";
        wholeRenderedText += ("class=\" x-component x-border-layout-ct\" ");
        wholeRenderedText += ("style=\"height: ") + height + "px; " + "background: none;\"";
        wholeRenderedText += (">");

        String specialClassForCheckbox = "";

        /*
         * render left component
         */

        renderer = getLeftRenderer();
        if (leftRegionSize > 0 && renderer != null) {
            if (renderer instanceof CheckboxHtmlGridCellRenderer<?>) {
                specialClassForCheckbox = "x-form-check-wrap";
            }

            // render left component
            renderer.setOwnerTree(ownerTree);
            String r = (String) renderer.render(model, property, config, rowIndex, colIndex, store, grid);

            // just create a DIV if there are some data
            if (GwtStringUtils.isBlank(r)) {
                wholeRenderedText += "&nbsp;";
            } else {
                // render left component layout
                wholeRenderedText += "<div class=\"" + specialClassForCheckbox
                        + " x-form-field x-component x-border-panel\" ";
                wholeRenderedText += "id=\"" + uniqueId + "-left" + "\" ";
                wholeRenderedText += "style=\"position: relative; left: 0px; top: 0px; ";
                wholeRenderedText += "width: " + leftRegionSize + "px; ";
                wholeRenderedText += "height: " + height + "px; ";
                wholeRenderedText += "\">";
                wholeRenderedText += (r);

                // close left component
                wholeRenderedText += ("</div>");
            }
        }

        /*
         * render right component
         */

        int rightCompLeft = leftRegionSize;
        int rightCompWidth = width - leftRegionSize;
        int remainingWidthInPercent = (int) ((float) rightCompWidth / width * MAX_PERCENTAGE_VALUE);

        specialClassForCheckbox = "";
        renderer = getRightRenderer();
        if (rightCompWidth > 0 && renderer != null) {
            if (renderer instanceof CheckboxHtmlGridCellRenderer<?>) {
                specialClassForCheckbox = "x-form-check-wrap";
            }

            // render right component
            renderer.setOwnerTree(ownerTree);
            String r = (String) renderer.render(model, property, config, rowIndex, colIndex, store, grid);
            if (GwtStringUtils.isBlank(r)) {
                wholeRenderedText += "&nbsp;";
            } else {

                // render right component layout
                wholeRenderedText += "<div class=\"" + specialClassForCheckbox
                        + " x-form-field x-component x-border-panel\" ";
                wholeRenderedText += "id=\"" + uniqueId + "-right\" ";
                wholeRenderedText += ("style=\"");
                wholeRenderedText += ("top: 0px; ");
                wholeRenderedText += "left: " + rightCompLeft + "px; ";
                wholeRenderedText += "height: " + height + "px; ";
                wholeRenderedText += "width: " + remainingWidthInPercent + "%; ";
                wholeRenderedText += "\">";
                wholeRenderedText += (r);

                // close right component
                wholeRenderedText += "</div>";
            }
        }

        // close container
        wholeRenderedText += "</div>";

        return wholeRenderedText;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected HtmlGridCellRenderer<M> getLeftRenderer() {
        return leftRenderer;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected HtmlGridCellRenderer<M> getRightRenderer() {
        return rightRenderer;
    }

}
